package com.wcl.demo.ui.adapter;

import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMGroup;
import com.easemob.chat.EMGroupManager;
import com.easemob.exceptions.EaseMobException;
import com.wcl.demo.R;
import com.wcl.demo.ui.fragment.ItemInterface.GroupOnItemListener;
import com.wcl.demo.view.CustomDialog;

import java.util.List;

/**
 * Created by dllo on 15/12/3.
 */
public class GroupMsgAdapter extends BaseAdapter {
    private List<String> datas;
    private String groupid;
    private String owner;

    public void addData(List<String> grouplist, String groupid, String owner) {
        this.datas = grouplist;
        this.groupid = groupid;
        this.owner = owner;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (datas != null && datas.size() > 0) {
            if (owner.equals(EMChatManager.getInstance().getCurrentUser())) {
                return datas.size() + 2;
            } else {
                return datas.size() + 1;
            }
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return datas != null && datas.size() > 0 ? datas.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        if (datas != null && datas.size() > 0) {
            ViewHolder holder = null;

            if (convertView == null) {
                convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.groupmsg_item, null);
                holder = new ViewHolder();
                holder.groupmsg_name = (TextView) convertView.findViewById(R.id.groupmsg_name);
                holder.groupmsg_header = (ImageView) convertView.findViewById(R.id.groupmsg_header);
                holder.groupmsg_layout = (LinearLayout) convertView.findViewById(R.id.groupmsg_layout);
                holder.groupmsg_add= (ImageView) convertView.findViewById(R.id.groupmsg_add);
                convertView.setTag(holder);

            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            if (position == datas.size() + 1) {
                holder.groupmsg_layout.setVisibility(View.GONE);
                holder.groupmsg_add.setVisibility(View.VISIBLE);
                holder.groupmsg_add.setImageResource(R.mipmap.group_remove);
            }else if(position==datas.size()){
                holder.groupmsg_layout.setVisibility(View.GONE);
                holder.groupmsg_add.setVisibility(View.VISIBLE);
                holder.groupmsg_add.setImageResource(R.mipmap.group_add);
            }else{
                String members = datas.get(position);
                holder.groupmsg_name.setText(members);
            }

        }
        return convertView;
    }

    class ViewHolder {
        private TextView groupmsg_name;
        private ImageView groupmsg_header;
        private LinearLayout groupmsg_layout;
        private ImageView groupmsg_add;
    }

}
